﻿using HDS.IService.Master;
using HDS.Model.Entity.Master;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using SqlSugar.Extensions;
using HDS.Common.Helper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using HDS.Model;
using HDS.Model.Entity.View;
using HDS.Admin.Api.ViewModels.SupplierHotel;
using HDS.IService.Log;
using HDS.Model.Entity.Log;
using System.Security.Claims;

namespace HDS.Admin.Api.Controllers
{
    [Route("supplier/[action]")]
    [ApiController]
    [EnableCors]
    [Authorize]
    public class SupplierController : AdminController
    {
        private readonly ILogger<SupplierController> _logger;
        private readonly ISupplierApiConfigService _ISupplierApiConfigService;
        private readonly ISupplierHotelService _ISupplierHotelService;
        private readonly IUpdateLogService _IUpdateLogService;

        public SupplierController(ILogger<SupplierController> logger, ISupplierApiConfigService isupplierApiConfigService, ISupplierHotelService isupplierHotelService, IUpdateLogService iupdateLogService)
        {
            _logger = logger;
            _ISupplierApiConfigService = isupplierApiConfigService;
            _ISupplierHotelService = isupplierHotelService;
            _IUpdateLogService = iupdateLogService;
        }

        public async Task<ResultData<DataPagingModel<VSupplierHotel>>> GetHotelList([FromBody] SearchDto searchDto)
        {
            var hotelNameEn = (searchDto.HotelName?? "").IsEnglish() ? searchDto.HotelName : "";
            var hotelNameCn = (searchDto.HotelName?? "").IsEnglish() ? "" : searchDto.HotelName;
            var dataPaging = await _ISupplierHotelService.GetHotelList(searchDto.PageIndex, searchDto.PageSize, searchDto.Supplier, searchDto.SupplierHotelId, searchDto.HotelId, searchDto.IsAvail, searchDto.IsTop, searchDto.IsMapping, searchDto.HotelStar, hotelNameEn, hotelNameCn, searchDto.CountryName, searchDto.CityName);

            return new ResultData<DataPagingModel<VSupplierHotel>> { Success = true, Data = dataPaging };
        }

        public async Task<ResultData<VSupplierHotel>> GetHotel(string supplier, string supplierHotelId)
        {
            var vHotel = await _ISupplierHotelService.GetVHotel(supplier, supplierHotelId);

            return new ResultData<VSupplierHotel> { Success = true, Data = vHotel };
        }

        public async Task<ResultData<DataPagingModel<SupplierRoom>>> GetRoomList([FromBody] SearchRoomDto searchDto)
        {
            var dataPaging = await _ISupplierHotelService.GetRoomList(searchDto.PageIndex, searchDto.PageSize, searchDto.Supplier, searchDto.SupplierHotelId, searchDto.HotelId);

            return new ResultData<DataPagingModel<SupplierRoom>> { Success = true, Data = dataPaging };
        }

        public async Task<ResultData<int>> DeleteRooms([FromBody] List<SupplierRoomDto> rooms)
        {
            foreach (var room in rooms)
            {
                if (await _ISupplierHotelService.DeleteRoom(room.Supplier, room.SupplierHotelId, room.SupplierRoomId))
                {
                    var vhotel = await _ISupplierHotelService.GetVHotel(room.Supplier, room.SupplierHotelId);
                    await _IUpdateLogService.Add(new UpdateLog
                    {
                        LogType = UpdateLogType.StaticData,
                        LogSubType = "Delete Supplier Room",
                        LogText = $"Delete Supplier Room, {room.Supplier}|{room.SupplierHotelId}|{room.SupplierRoomId}",
                        BusinessId = vhotel.HotelId.HasValue ? vhotel.HotelId.Value.ToString() : "",
                        UpdatedBy = this.UserId,
                        UpdatedByName = this.UserName,
                        UpdatedTime = DateTime.Now
                    });
                }
            }

            return new ResultData<int> { Success = true };
        }

        public async Task<ResultData<int>> RestoreRooms([FromBody] List<SupplierRoomDto> rooms)
        {
            foreach (var room in rooms)
            {
                if (await _ISupplierHotelService.RestoreRoom(room.Supplier, room.SupplierHotelId, room.SupplierRoomId))
                {
                    var vhotel = await _ISupplierHotelService.GetVHotel(room.Supplier, room.SupplierHotelId);
                    await _IUpdateLogService.Add(new UpdateLog
                    {
                        LogType = UpdateLogType.StaticData,
                        LogSubType = "Restore Supplier Room",
                        LogText = $"Restore Supplier Room, {room.Supplier}|{room.SupplierHotelId}|{room.SupplierRoomId}",
                        BusinessId = vhotel.HotelId.HasValue ? vhotel.HotelId.Value.ToString() : "",
                        UpdatedBy = this.UserId,
                        UpdatedByName = this.UserName,
                        UpdatedTime = DateTime.Now
                    });
                }
            }

            return new ResultData<int> { Success = true };
        }

        public async Task<ResultData<List<SupplierApiConfig>>> GetConfigList()
        {
            var list = await _ISupplierApiConfigService.GetList(null);

            return new ResultData<List<SupplierApiConfig>> { Success = true, Data = list };
        }
    }
}
